package com.hxkcloud.nurse.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.PatientEvalutionBean;
import com.hxkcloud.nurse.viewholders.DpatientEvalutionViewholder;

import java.util.List;

/**
 * Created by william_sun on 2016-10-11.
 * Description:
 */
public class DpatientEvalutionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<PatientEvalutionBean> datas;
    private LayoutInflater mInflater;
    private evalutionListener listener;

    public DpatientEvalutionAdapter(Context mContext, List<PatientEvalutionBean> datas, evalutionListener listener) {
        this.mContext = mContext;
        this.datas = datas;
        this.listener = listener;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setData(List<PatientEvalutionBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_dpatientevalution, null);
        DpatientEvalutionViewholder holder = new DpatientEvalutionViewholder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        DpatientEvalutionViewholder viewholder = (DpatientEvalutionViewholder) holder;
        PatientEvalutionBean data = datas.get(position);
        viewholder.tv_date.setText(data.getDate().split(" ")[0]);
        viewholder.iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onDelete(position);
            }
        });
        viewholder.tv_content.setText(data.getContent());
        if (TextUtils.isEmpty(data.getVoiceUrl())) {
            viewholder.ll_voice.setVisibility(View.GONE);
        } else {
            viewholder.ll_voice.setVisibility(View.VISIBLE);
            viewholder.ll_voice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onVoice(position);
                }
            });
            if (data.isVoicePlaying()) {
                AnimationDrawable animDrawable = (AnimationDrawable) ContextCompat.getDrawable(mContext, R.anim.anim_voice);
                animDrawable.start();
                viewholder.iv_voice.setImageDrawable(animDrawable);

            } else {
                viewholder.iv_voice.setImageResource(R.drawable.report_voice);
            }
            viewholder.tv_voice.setText("‘’" + data.getVoiceLength());
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public interface evalutionListener {
        void onDelete(int position);//点击删除


        void onVoice(int position);//听录音
    }
}
